package com.gxa.community.admin.controller.mine;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gxa.community.admin.service.mine.MineService;
import com.gxa.community.admin.service.order.AfterSalesService;
import com.gxa.community.admin.service.order.OrderService;
import com.gxa.community.core.pojo.vo.order.AfterSalesVo;
import com.gxa.community.core.pojo.vo.order.OrderInfoVo;
import com.gxa.community.core.utils.LittleUtils;
import com.gxa.community.core.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mine")
public class MineController {
    // TODO uid不应该由请求带过来, 应该从token中取

    final MineService mineService;
    final OrderService orderService;
    final AfterSalesService afterSalesService;

    public MineController(MineService mineService, OrderService orderService, AfterSalesService afterSalesService) {
        this.mineService = mineService;
        this.orderService = orderService;
        this.afterSalesService = afterSalesService;
    }

    /**
     * 查询『我的』界面基础信息
     */
    @GetMapping("/getMineInfo")
    public R getMineInfo(Integer uid) {
        if (uid == null || uid < 1) return R.error("用户信息有误");
        return mineService.getMineInfo(uid);
    }

    /**
     * 查询我的优惠券
     */
    @GetMapping("/queryCoupon")
    public R queryCoupon(Integer uid, Integer status) {
        if (uid == null || uid < 1) return R.error("用户信息有误");
        return mineService.queryCoupon(uid, status);
    }

    /**
     * 查询用户订单
     *
     * @return 订单列表
     */
    @GetMapping("/queryOrder")
    public R queryOrder(Integer page, Integer size, @RequestBody OrderInfoVo orderInfoVo) {
        // 数据校验
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1 || size > 100) size = 5;

        if (orderInfoVo.getBuyerId() == null || orderInfoVo.getBuyerId() < 1) return R.error("用户信息有误");
        if (LittleUtils.checkNum(orderInfoVo.getStatus(), 1, 5)) return R.error("订单状态有误");

        return orderService.queryOrder(page, size, orderInfoVo);
    }

    /**
     * 查询售后信息
     */
    @GetMapping("/queryAfterSales")
    public R queryAfterSales(Integer page, Integer size, @RequestBody AfterSalesVo afterSalesVo) {
        if (afterSalesVo.getUserId() == null || afterSalesVo.getUserId() < 1) return R.error("用户信息有误");
        if (LittleUtils.checkNum(afterSalesVo.getType(), 1, 3)) R.error("售后类型有误");

        return afterSalesService.queryAfterSales(page, size, afterSalesVo);
    }

    /**
     * 新增售后申请
     */
    @PostMapping("/addAfterSales")
    public R addAfterSales(@RequestBody AfterSalesVo salesVo) {
        if (salesVo.getUserId() == null || salesVo.getUserId() < 1) return R.error("用户信息有误");
        if (StringUtils.isBlank(salesVo.getOrderNo())) return R.error("订单编号不能为空");
        if (LittleUtils.checkNum(salesVo.getType(), 1, 3)) R.error("售后类型有误");
        if (StringUtils.isBlank(salesVo.getDesc())) return R.error("问题描述不能为空");
        if (StringUtils.isBlank(salesVo.getPic())) return R.error("截图凭证不能为空");
        if (LittleUtils.checkNum(salesVo.getRequestReason(), 1, 5)) return R.error("退款原因有误");

        return afterSalesService.addAfterSales(salesVo);
    }

    @PostMapping("/queryGoods")
    public R queryGoods(Integer uid, @RequestBody List<String> orderNo) {
        if (uid == null || uid < 1) return R.error("用户信息有误");
        return mineService.queryGoods(uid, orderNo);
    }
}
